package com.wanli.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanli.common.Result;
import com.wanli.domain.dto.GoodsDto;
import com.wanli.domain.entity.Goods;
import com.wanli.domain.entity.SysGoodsCategoryBusiness;
import com.wanli.domain.vo.BACVo;
import com.wanli.domain.vo.DictVo;
import com.wanli.mapper.BusinessMapper;
import com.wanli.mapper.CategoryMapper;
import com.wanli.mapper.SysGoodsCategoryBusinessMapper;
import com.wanli.service.GoodsService;
import com.wanli.mapper.GoodsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 35747
 * @description 针对表【sys_goods】的数据库操作Service实现
 * @createDate 2024-11-09 20:18:41
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
        implements GoodsService {
    @Resource
    private SysGoodsCategoryBusinessMapper sysGoodsCategoryBusinessMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public Result saveGoods(GoodsDto dto) {
        Goods goods = new Goods();
        BeanUtil.copyProperties(dto, goods);
        goods.setCreated(LocalDateTime.now());
        goods.setUpdated(LocalDateTime.now());
        if (dto.getGoodsCover() == null) {
            // 如果上传图片为空，给默认的
            goods.setGoodsCover("https://alextachalova.com/wp-content/themes/wp-wellow/assets/images/no-image.jpg");
        }
        // 新增餐品
        goodsMapper.insert(goods);
        Long goodsId = goodsMapper.selectOne(new QueryWrapper<Goods>().eq("goods_name", goods.getGoodsName())).getGoodsId();
        // 新增关系表
        SysGoodsCategoryBusiness obj = new SysGoodsCategoryBusiness();
        obj.setGoodsId(goodsId);
        obj.setCategoryId(dto.getCategoryId());
        obj.setBusinessId(dto.getBusinessId());
        sysGoodsCategoryBusinessMapper.insert(obj);
        return Result.success("餐品信息保存成功");
    }

    @Override
    public Result updateGoods(GoodsDto dto) {
        // 更新餐品
        Goods goods = new Goods();
        BeanUtil.copyProperties(dto, goods);
        goods.setUpdated(LocalDateTime.now());
        goodsMapper.update(goods, new QueryWrapper<Goods>().eq("goods_id", dto.getGoodsId()));
        // 删除之前的关系表
        LambdaQueryWrapper<SysGoodsCategoryBusiness> query = new LambdaQueryWrapper<>();
        query.eq(SysGoodsCategoryBusiness::getGoodsId, dto.getGoodsId());
        sysGoodsCategoryBusinessMapper.delete(query);

        // 新建关系表
        SysGoodsCategoryBusiness obj = new SysGoodsCategoryBusiness();
        obj.setGoodsId(dto.getGoodsId());
        obj.setCategoryId(dto.getCategoryId());
        obj.setBusinessId(dto.getBusinessId());
        sysGoodsCategoryBusinessMapper.insert(obj);
        return Result.success("餐品数据更新成功");

    }

    /**
     * 获取种类与商品列表字典
     * @return
     */
    @Override
    public Result getDict() {
        BACVo vo = new BACVo();
        List<DictVo> categoryDict = categoryMapper.selectList(null).stream()
                .map(item -> {
                    DictVo dv = new DictVo();
                    dv.setValue(String.valueOf(item.getCategoryId()));
                    dv.setLabel(item.getCategoryName());
                    return dv;
                }).collect(Collectors.toList());
        List<DictVo> businessDict = businessMapper.selectList(null).stream()
                .map(item -> {
                    DictVo dv = new DictVo();
                    dv.setValue(String.valueOf(item.getBusinessId()));
                    dv.setLabel(item.getBusinessName());
                    return dv;
                }).collect(Collectors.toList());
        vo.setCategoryList(categoryDict);
        vo.setBusinessList(businessDict);
        return Result.success(vo);
    }

    @Override
    public Result getDictById(String goodsId) {
        SysGoodsCategoryBusiness sysGoodsCategoryBusiness = sysGoodsCategoryBusinessMapper.selectOne(new QueryWrapper<SysGoodsCategoryBusiness>()
                .eq("goods_id", goodsId));
        if (sysGoodsCategoryBusiness != null) {
            return Result.success(sysGoodsCategoryBusiness);
        } else {
            return Result.fail("该商品没有关联信息");
        }
    }
}




